package com.fare.taluo.application.impl.analyse;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.foreveross.hbase.client.HBaseHelper;
import org.foreveross.hbase.client.HBasePlaneinfoHelper;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.fare.taluo.vo.core.PlaneInfo;
import com.fare.taluo.application.FreightPriceApplication;
import com.fare.taluo.application.analyse.PriceSearchApplication;

@Scope("singleton")
@Service("priceSearchApplication")
public class PriceSearchApplicationImpl implements PriceSearchApplication {
	@Resource(name="freightPriceApplication")
	private FreightPriceApplication freightPriceApplication;
	public List<PlaneInfo> search(String tableName,String startRowKey,String endRowKey,List<Filter> filters) throws IOException{
		List<PlaneInfo> list=new ArrayList<PlaneInfo> ();
		//航班最低价LP_FLIGHT：  渠道_起点(使用三字码)_终点(使用三字码)_(航班号)_航班日期(年月日时)
		//LP_LINE  渠道_起点(使用三字码)_终点(使用三字码)_航班日期(年月日时)
		ResultScanner scan=HBaseHelper.getRecords(tableName, startRowKey, endRowKey,null,filters);
		///HBaseHelper.printResult(scan);
		list=HBasePlaneinfoHelper.transformHBaseResultToPlaneinfo(scan);
		return list;
		
	}
	public List<PlaneInfo> search(String startRowKey,String endRowKey,List<Filter> filters) throws Exception{
		List<PlaneInfo> list=new ArrayList<PlaneInfo> ();
		//航班最低价LP_FLIGHT：  渠道_起点(使用三字码)_终点(使用三字码)_(航班号)_航班日期(年月日时)
		//LP_LINE  渠道_起点(使用三字码)_终点(使用三字码)_航班日期(年月日时)
      
		System.out.println(startRowKey+"   "+ endRowKey);
		list = freightPriceApplication.findByConditionsToPlaneInfo("LP_FLIGHT_DATE", startRowKey, endRowKey, filters);
		return list;
		
	}
	
	public List<PlaneInfo> searchByContidions(String tableName,String startRowKey,String endRowKey,List<Filter> filters){
		List<PlaneInfo> list=new ArrayList<PlaneInfo> ();
		//航班最低价LP_FLIGHT：  渠道_起点(使用三字码)_终点(使用三字码)_(航班号)_航班日期(年月日时)
		//LP_LINE  渠道_起点(使用三字码)_终点(使用三字码)_航班日期(年月日时)
      
	//	System.out.println(startRowKey+"   "+ endRowKey);
		list = freightPriceApplication.findByConditionsToPlaneInfo(tableName, startRowKey, endRowKey, filters);
		return list;
		
	}
	@Override
	public List<PlaneInfo> getPlaneInfos(String date, String rowKeyHead,
			String checkBox_str,String companyCode) throws Exception {
		List<Filter> filters=new ArrayList<Filter>();
		//filters.add(new SingleColumnValueFilter("planeinfo".getBytes(),"carrierKey".getBytes(),CompareOp.EQUAL,companyCode.getBytes()));
		if(checkBox_str.length()==1){//单选
			return this.getOnePlaneInfos(date, rowKeyHead, PriceSearchChoseEnum.valueOf(checkBox_str),filters);
		}else{//组合
			List<List<PlaneInfo>> lists=new ArrayList();
			for (int i = 0; i < checkBox_str.length(); i++) {
				lists.add(this.getOnePlaneInfos(date, rowKeyHead, PriceSearchChoseEnum.valueOf(checkBox_str.charAt(i)+""),filters));
			}
			List<PlaneInfo> listInfo=new ArrayList();
			for (Iterator iterator = lists.iterator(); iterator.hasNext();) {
				List<PlaneInfo> list = (List<PlaneInfo>) iterator.next();
				for (Iterator iterator2 = list.iterator(); iterator2.hasNext();) {
					PlaneInfo planeInfo = (PlaneInfo) iterator2.next();
					if(planeInfo!=null){
						listInfo.add(planeInfo);
					}
				}
			}
			return listInfo;
		}
			
	}
	public static void main(String[] args) throws Exception {
		String   endRowKey="2000507CANPEK20130304201301040800000000";
				            
		String startRowKey="2000507CANPEK20130304201303040800zz0000";
		PriceSearchApplicationImpl imp=new PriceSearchApplicationImpl();
		List<Filter> filters=new ArrayList();
		filters.add(new SingleColumnValueFilter("planeinfo".getBytes(),"carrierKey".getBytes(),CompareOp.EQUAL,"ZH".getBytes()));
		//0000102DLCSZX20130304201303171200ZH9830//0000102DLCSZX20130304201303200000000000
		List<PlaneInfo> list=imp.search("2000001PEKSZX20130315201303210000000000", "2000001PEKSZX20130315201303212300zz0000",filters);
		
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			PlaneInfo planeInfo = (PlaneInfo) iterator.next();
			System.out.println(planeInfo.getFlightBatch() );
		}
		System.out.println(list.size());
	}

	private List<PlaneInfo> getOnePlaneInfos(String dates,String rowKeyHead,PriceSearchChoseEnum choseEnum,List<Filter> filters)throws Exception{
		//单选
		return choseEnum.getPlaneInfos(dates, rowKeyHead,filters);
	}
	
	@Override
	public List<PlaneInfo> getPlaneInfos(String dates, String rowKeyHead,
			String checkBox_str, String timeBegin, String timeEnd,
			String companyCode) throws Exception {
		List<Filter> filters=new ArrayList();
		filters.add(new SingleColumnValueFilter("planeinfo".getBytes(),"carrierKey".getBytes(),CompareOp.EQUAL,companyCode.getBytes()));
		return this.getMyPlaneInfos(dates, rowKeyHead,PriceSearchChoseEnum.valueOf(checkBox_str),timeBegin,timeEnd,filters);
	}
	
	private List<PlaneInfo> getMyPlaneInfos(String dates,String rowKeyHead,PriceSearchChoseEnum choseEnum,String timeBegin, String timeEnd,List<Filter> filters)throws Exception{
		//自定义时间单选
		return choseEnum.getPlaneInfos(dates, rowKeyHead,timeBegin,timeEnd,filters);
}
	
}
